//
//  SS_MainViewController.swift
//  SSNews
//
//  Created by 叶竞泽 on 2017/5/1.
//  Copyright © 2017年 叶竞泽. All rights reserved.
//

import UIKit

fileprivate let mainCellIdentifier = "MainCell"

class SS_MainViewController: UIViewController {
    
    // MARK: - 属性
    
    @IBOutlet weak var topicView: SS_TopicView! {
        didSet {
            topicView.buttonClickBlock = { (index) in
                let offsetX = CGFloat(index) * mainWidth
                self.mainCollectionView?.setContentOffset(CGPoint(x: offsetX, y: 0), animated: true)
            }
        }
    }
    
    @IBOutlet weak var containerView: UIView!
    
    /// 主列表
    var mainCollectionView: UICollectionView?
    
    /// 列表分类缓存
    let newsTopicKeys: [String] = ["top", "yule", "shehui", "keji", "shishang", "tiyu",  "guonei", "guoji", "junshi", "caijing"]
    
    var channelVCCache: [String: SS_ChannelViewController] = [String: SS_ChannelViewController]()
    
    // MARK: - 生命周期
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        setupMainCollectionView()
    }
}

// MARK: - 设置界面
extension SS_MainViewController {
    
    fileprivate func setupUI() {
        
        automaticallyAdjustsScrollViewInsets = false
        view.bounds.size.height -= (tabBarController?.tabBar.bounds.height ?? 0) + (navigationController?.navigationBar.bounds.height ?? 0) + topicView.bounds.height + UIApplication.shared.statusBarFrame.height
        title = "新闻"
        
        // FIXME: - tabbarItem图片
    }
    
    /// 创建控制器并缓存
    fileprivate func createChannelVC(withString string: String, inCell cell: UICollectionViewCell) -> SS_ChannelViewController {
        if newsTopicKeys.contains(string) {
            let vc = SS_ChannelViewController()
            vc.type = string
            vc.view.frame = cell.bounds
            vc.view.backgroundColor = cell.contentView.backgroundColor
            vc.block = { [weak self] (vc) in
                self?.navigationController?.pushViewController(vc, animated: true)
            }
            channelVCCache[string] = vc
            return vc
        }
        return SS_ChannelViewController()
    }
    
    fileprivate func setupMainCollectionView() {
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: view.bounds.width, height: view.bounds.height)
        layout.scrollDirection = .horizontal
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        mainCollectionView = UICollectionView(frame: view.bounds, collectionViewLayout: layout)
        mainCollectionView?.backgroundColor = UIColor.blue
        mainCollectionView?.dataSource = self
        mainCollectionView?.delegate = self
        mainCollectionView?.contentSize = CGSize(width: mainWidth * CGFloat(newsTopicKeys.count), height: 0)
        mainCollectionView?.register(UICollectionViewCell.self, forCellWithReuseIdentifier: mainCellIdentifier)
        mainCollectionView?.backgroundColor = UIColor.white
        mainCollectionView?.isPagingEnabled = true
        mainCollectionView?.bounces = false
        containerView.addSubview(mainCollectionView!)
        containerView.backgroundColor = UIColor.black
    }
}

extension SS_MainViewController: UICollectionViewDataSource, UICollectionViewDelegate {
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let index = Int(scrollView.contentOffset.x) / Int(mainWidth)
        topicView.currentButtonIndex = index
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return newsTopicKeys.count
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let _ = collectionView.cellForItem(at: indexPath)
        // TODO: 实现点击事件
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: mainCellIdentifier, for: indexPath)
        for subView in cell.contentView.subviews {
            subView.removeFromSuperview()
        }
        let vc = createChannelVC(withString: newsTopicKeys[indexPath.row], inCell: cell)
        cell.contentView.addSubview(vc.view)
        
        return cell
    }
}
